package com.demo.controller;


import com.demo.pojo.Competence;
import com.demo.service.CompetenceService;
import com.demo.service.UserService;
import com.demo.pojo.User;
import com.demo.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author： Administrator
 * @date: 21/05/07 18:15
 * 描述：
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    CompetenceService competenceService;

    @RequestMapping("/page.do")
    @ResponseBody
    public Map<String, Object> page(
            @RequestParam(value = "username", required = false, defaultValue = "") String username,
            @RequestParam(value = "competence", required = false) Integer competence,
            @RequestParam(value = "stateOn", required = false) String stateOn,
            @RequestParam(value = "stateOff", required = false) String stateOff,
            @RequestParam(value = "pageNo", required = false, defaultValue = "0") int pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "0") int pageSize) {
        Map<String, Object> map = new HashMap<>();
        User user = new User();
        map.put("code", 0);
        map.put("message", "");
        user.setUsername("%" + username + "%");
        user.setCompetenceId(competence);
        if (stateOn != null && stateOff == null) {
            user.setState(true);
        } else if (stateOff != null && stateOn == null) {
            user.setState(false);
        }
        Page<User> page = userService.page(user, pageNo, pageSize);
        map.put("count", page.getPageTotalCount());
        map.put("data", page.getItems());
        return map;
    }

    @PostMapping("/add.do")
    @ResponseBody
    public Map<String, Object> add(HttpSession session, User user) {
        Map<String, Object> map = new HashMap<>();
        User currentUser = (User) session.getAttribute("user");
        if (currentUser.getCompetenceId() < 1) {
            map.put("code", 4);
            map.put("message", "你没有权限添加用户！");
            return map;
        }
        int add;
        try {
            add = userService.add(user);
        } catch (Exception e) {
            map.put("code", -1);
            map.put("message", "服务器发生异常！");
            return map;
        }
        switch (add) {
            case -2:
                map.put("code", 2);
                map.put("message", "用户名重复！");
                return map;
            case -1:
                map.put("code", 3);
                map.put("message", "员工编号不存在！");
                return map;
            case 0:
                map.put("code", 1);
                map.put("message", "添加失败！");
                return map;
            case 1:
                map.put("code", 0);
                map.put("message", "添加成功！");
                return map;
            default:
                map.put("code", -1);
                map.put("message", "未知错误！");
                return map;
        }
    }

    @PostMapping("/state.do")
    @ResponseBody
    public Map<String, Object> state(
            HttpSession session,
            @RequestParam(value = "id") String username,
            @RequestParam(value = "state") Boolean state) {
        Map<String, Object> map = new HashMap<>();
        User user = (User) session.getAttribute("user");
        if (user.getUsername().equals(username)) {
            map.put("code", 5);
            map.put("message", "你无法停用自己！");
            return map;
        }
        if (user.getCompetenceId() < 1) {
            map.put("code", 4);
            map.put("message", "你没有权限修改权限！");
            return map;
        }
        User userState = new User();
        userState.setUsername(username);
        userState.setState(state);
        int update = userService.update(userState);
        switch (update) {
            case -3:
                map.put("code", 3);
                map.put("message", "该用户不存在，可能已被删除，请刷新页面！");
                return map;
            case -2:
                map.put("code", 2);
                map.put("message", "你没有权限修改该用户权限！");
                return map;
            case 0:
                map.put("code", 1);
                map.put("message", "修改权限失败！");
                return map;
            case 1:
                map.put("code", 0);
                map.put("message", state ? "已启用！" : "已停用！");
                return map;
            default:
                map.put("code", -1);
                map.put("message", "未知错误！");
                return map;
        }
    }

    @PostMapping("/delete.do")
    @ResponseBody
    public Map<String, Object> del(HttpSession session, String username) {
        Map<String, Object> map = new HashMap<>();
        User user = (User) session.getAttribute("user");
        if (user.getCompetenceId() < 1) {
            map.put("code", 3);
            map.put("message", "你没有权限删除用户！");
            return map;
        }
        if (user.getUsername().equals(username)) {
            map.put("code", 2);
            map.put("message", "你无法删除自己！");
            return map;
        }
        int del = userService.del(username);
        switch (del) {
            case -3:
                map.put("code", 4);
                map.put("message", "该用户不存在，可能已被删除，请刷新页面！");
                return map;
            case -2:
                map.put("code", 3);
                map.put("message", "无法删除管理员！");
                return map;
            case -1:
                map.put("code", 2);
                map.put("message", "该用户还有员工正在使用，无法删除！");
                return map;
            case 0:
                map.put("code", 1);
                map.put("message", "删除失败！");
                return map;
            case 1:
                map.put("code", 0);
                map.put("message", "删除成功！");
                return map;
            default:
                map.put("code", -1);
                map.put("message", "未知错误！");
                return map;
        }
    }

    @RequestMapping("/select.do")
    @ResponseBody
    public Map<String, Object> select(User user) {
        Map<String, Object> map = new HashMap<>();
        if (user.getUsername() == null) {
            map.put("code", 1);
            map.put("message", "请输入要查询用户名！");
            return map;
        }
        user = userService.select(user);
        if (user == null) {
            map.put("code", 2);
            map.put("message", "未查询到该用户的信息，可能是已被删除！");
            return map;
        }
        map.put("code", 0);
        map.put("message", "");
        map.put("data", user);
        return map;
    }

    @PostMapping("/update.do")
    @ResponseBody
    public Map<String, Object> update(HttpSession session, User userUpdate) {
        Map<String, Object> map = new HashMap<>();
        if (userUpdate.getUsername() == null) {
            map.put("code", 7);
            map.put("message", "请正确的访问该方法！");
            return map;
        }
        User user = (User) session.getAttribute("user");
        if (user.getCompetenceId() < 1) {
            map.put("code", 6);
            map.put("message", "你没有权限修改用户！");
            return map;
        }
        if (user.getCompetenceId() <= userUpdate.getCompetenceId()) {
            map.put("code", 5);
            map.put("message", "你无法将对方权限提升至你的权限及之上！");
            return map;
        }
        int update = userService.update(userUpdate);
        switch (update) {
            case -3:
                map.put("code", 4);
                map.put("message", "该用户不存在，可能已被删除，请刷新页面！");
                return map;
            case -2:
                map.put("code", 3);
                map.put("message", "你没有权限编辑该用户！");
                return map;
            case -1:
                map.put("code", 2);
                map.put("message", "员工编号不存在！");
                return map;
            case 0:
                map.put("code", 1);
                map.put("message", "修改信息失败！");
                return map;
            case 1:
                map.put("code", 0);
                map.put("message", "修改信息成功！");
                return map;
            default:
                map.put("code", -1);
                map.put("message", "未知错误！");
                return map;
        }
    }

    @RequestMapping("/competenceList.do")
    @ResponseBody
    public List<Competence> list() {
        return competenceService.list();
    }

    private User queryByUsername(String username) {
        User user = new User();
        user.setUsername(username);
        return userService.select(user);
    }
}
